package com.maple.phoneward.custom;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.maple.phoneward.activity.R;

/**
 * MainAdapter.java
 * 
 * @author: YaJian Qiu
 * @Time 2013-3-19 上午9:10:31<br>
 *       本类描述: 自定义ListView适配器
 */
public class MainAdapter extends BaseAdapter {

	private Context context;

	/**
	 * 要显示的数据
	 */
	private String[] data;

	public MainAdapter(Context context) {
		this.context = context;
		this.data = context.getResources().getStringArray(R.array.main_menu_names);// 从资源文件中加载要显示的数据
	}

	@Override
	public int getCount() {
		return data.length;
	}

	@Override
	public Object getItem(int position) {
		return data[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			// 加载Item的布局
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.main_list_view_item, null);
			viewHolder.name = (TextView) convertView.findViewById(R.id.mailn_list_view_item_name);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.name.setText(data[position]);

		return convertView;
	}

	class ViewHolder {
		TextView name;
	}
}
